﻿//printGirdObj({
//    containerId: 'tableArea',//包含表格容器のID
//    title: '餐厅配送统计',
//    showRowNum: true,  //表格中rownumbers: true时为true
//    showOrder: false,//是否显示序号，rownumbers: true时有效，默认false
//    printInit: { //打印初始化设置参数
//        portrait: false //true纵向,false横向
//    }
//});

var PrintGrid = {};
PrintGrid.htmlMsg = null;
PrintGrid.title = null;
PrintGrid.printInit = null;
var getInfo = function () {
    var html = PrintGrid.htmlMsg ? PrintGrid.htmlMsg : "";

    /*****************隐藏某些列*****************/
    if (!$.isEmptyObject(PrintGrid.hideCols)) {
        var $table = $("<div>" + html + "</div>");
        var arr = PrintGrid.hideCols.split(',');
        for (var i = 0; i < arr.length; i++) {
            $table.find('tr').find("td:eq(" + arr[i] + ")").hide();
            $table.find('tr').find("th:eq(" + arr[i] + ")").hide();
        }
        html = $table.html();
    }
    /*****************隐藏某些列*****************/

    return html;
};
var getTitle = function () {
    return PrintGrid.title;
};
var getprintInit = function () {
    return PrintGrid.printInit;
};
function printGirdObj(printObj) {

    var containerId = printObj.containerId ? printObj.containerId : '';
    PrintGrid.title = printObj.title ? printObj.title : '';
    PrintGrid.printInit = printObj.printInit ? printObj.printInit : {};//打印初始化の参数
    PrintGrid.hideCols = printObj.hideCols ? printObj.hideCols : "";
    PrintGrid.htmlMsg = printObj.htmlMsg ? printObj.htmlMsg : "";
    PrintGrid.userWidth = printObj.userWidth ? printObj.userWidth : false;
    PrintGrid.ifPrintTableName = printObj.ifPrintTableName ? printObj.ifPrintTableName : false;//是否打印table的caption属性
    PrintGrid.tableCount = printObj.tableCount ? printObj.tableCount : 0;//同一个页面多个表时，第一个表从0开始，然后1,2,3，则总共打印4个表，但tableCount这里应填3
    var showOrder = printObj.showOrder ? printObj.showOrder : false;//是否显示序号
    var ordNum = 0;

    //	var initStr=JSON.stringify(printInit);

    var rownumbers = true;
    if (printObj.showRowNum == false) {
        rownumbers = false;
    }
    for (var i = 0; i <= PrintGrid.tableCount; i++) {
        var containerobj = $('#' + containerId);
        if (i > 0) containerobj = $('#' + containerId + i);
        if (PrintGrid.ifPrintTableName) {//多个表之间的标题
            PrintGrid.htmlMsg += '<span style="font-size:18px;">' + PrintGrid.title + '</span>';
        }
        PrintGrid.htmlMsg += '<table width="100%" cellspacing="0"  cellpadding="0" align="center" border="1">';
        /***********************【列头】>>开始*************************/
        var headTable = containerobj.find('.table');
        //		var tr = headTable.find('TR');
        var tr = headTable.find('thead:not(.disnot)');
        var trNum = 0;
        tr.each(function (index, element) {
            var th;
            if (rownumbers == false) {
                th = $(element).find('TH'); //后期有序号再说$(element).find('TH:not(:first-child)');
            } else {
                th = $(element).find('TH');
            }
            trNum = index;
            PrintGrid.htmlMsg += '<tr bgcolor="#E8E8D0">';//这一列加底色
            th.each(function (thindex, thelement) {
                if (thindex == 0 && rownumbers && !showOrder) {
                    ordNum = 1;
                }
                if (thindex >= ordNum || trNum > 1) {
                    if ($(thelement).attr('id') != null && $(thelement).attr('id').indexOf('_cb') != -1) {//checkbox不需要打印,所以起列名时不能用cb
                        //break;
                        return true;
                    }
                    var content = $(thelement).text();
                    var colSpan = $(thelement).attr('colSpan');
                    var rowSpan = $(thelement).attr('rowSpan');
                    content = content.replace(/(^\s*)|(\s*$)/g, "");//去掉空格
                    content = content.replace(/^(\s|\u00A0)+/, '').replace(/(\s|\u00A0)+$/, '');//去掉空格
                    if ((index == 0 && rownumbers && trNum <= 1)) {
                        content = '序号';
                    }
                    if (PrintGrid.userWidth) {
                        var w = $(thelement).width();
                        PrintGrid.htmlMsg += '<th align="center" width="' + w + '" colSpan="' + colSpan + '" rowSpan="' + rowSpan + '">' + content + '</th>';
                    } else {
                        PrintGrid.htmlMsg += '<th align="center" colSpan="' + colSpan + '" rowSpan="' + rowSpan + '">' + content + '</th>';
                    }
                }
            });
            PrintGrid.htmlMsg += '</tr>';
            //			if(index>0){//第一行为空
            //			}
        });
        /*************************【列头】>>结束*************************/
        /*************************【内容】>>begin************************/
        var bodyTable = containerobj.find('.table');
        var tr;
        tr = bodyTable.find('tbody tr:not(.footer,.tb-child)');
        tr.each(function (index, element) {
            var td = $(element).find('TD:visible');
            PrintGrid.htmlMsg += '<tr>';
            td.each(function (index, element) {
                if (index >= ordNum) {
                    //if ($(element).attr('aria-describedby').indexOf('_cb') != -1) {//checkbox不需要打印
                    //    //break;
                    //    return true;
                    //}
                    var align = $(element).css('text-align');
                    var colSpan = $(element).attr('colSpan');
                    var rowSpan = $(element).attr('rowSpan');
                    var content = $(element).text();
                    var img = $(element).find("img.export");
                    content = content.replace(/(^\s*)|(\s*$)/g, "");//去掉空格
                    content = content.replace(/^(\s|\u00A0)+/, '').replace(/(\s|\u00A0)+$/, '');//去掉空格
                    if (content == '') content = '&nbsp;';
                    if (img.length > 0) {
                        content = $(element).html();
                    }
                    PrintGrid.htmlMsg += '<td align="' + align + '" colSpan="' + colSpan + '" rowSpan="' + rowSpan + '">' + content + '</td>';
                }
            });
            PrintGrid.htmlMsg += '</tr>';
            //			if(index>0){//第一行为空
            //			}
        });
        /*************************【内容】<<end*************************/
        /*************************【合计】>>begin***********************/
        var footTable = containerobj.find('.table');;
        if (footTable) {
            var tr;
            tr = footTable.find('.footer');
            tr.each(function (index, element) {
                var td = $(element).find('TD:visible');
                PrintGrid.htmlMsg += '<tr>';
                td.each(function (index, element) {
                    if (index >= ordNum) {
                        //if ($(element).attr('aria-describedby').indexOf('_cb') != -1) {//checkbox不需要打印
                        //    //break;
                        //    return true;
                        //}
                        var align = $(element).css('text-align');
                        var colSpan = $(element).attr('colspan');
                        var rowSpan = $(element).attr('rowspan');
                        //						var content = $(element).text();
                        var content = $(element).html();
                        content = content.replace(/(^\s*)|(\s*$)/g, "");//去掉空格
                        content = content.replace(/^(\s|\u00A0)+/, '').replace(/(\s|\u00A0)+$/, '');//去掉空格
                        PrintGrid.htmlMsg += '<td align="' + align + '" colSpan="' + colSpan + '" rowSpan="' + rowSpan + '"><b>' + content + '</b></td>';
                    }
                });
                PrintGrid.htmlMsg += '</tr>';
            });
        }
        /*************************【合计】<<end*************************/

        PrintGrid.htmlMsg += '</table></br>';
    }
    var rand = Math.round(Math.random() * 10000);
    var winname = "print" + rand;
    var pwin = window.open("/Print", winname);

};
